﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CP.Utility;
using CP.Controllers.GPM;
using CP.Entities;

namespace GPM
{
    public partial class FrmSetRoleUser : BaseCommForm
    {
        public GridCheckMarksSelection selection;
        public User_Group CurrentUserGroup { get; private set; }
        public FrmSetRoleUser()
        {
            InitializeComponent();
            this.BarComponents = components;
        }

        public FrmSetRoleUser(User_Group group)
            : this()
        {
            this.CurrentUserGroup = group;
            this.txtGroupName.Text = group.group_name;
        }

        private void FrmSetRoleUser_Load(object sender, EventArgs e)
        {
            selection = new GridCheckMarksSelection(this.gvUserInfo);
            selection.SelectionChanged += new DevExpress.Data.SelectionChangedEventHandler(selection_SelectionChanged);
            SetRoleUserController controller = new SetRoleUserController();

            this.lookupDeptID.DataSource = controller.GetAllDepartments();
            this.lookupGroupID.DataSource = controller.GetAllGroups();
            IList<User_Info> users = controller.GetAllUsers();
            this.gcUserInfo.DataSource = users;
            IList<string> userids = new List<string>();
            users.ForEach(u => userids.Add(u.user_id));
            SetGroupChecked(userids, this.CurrentUserGroup.group_id);
        }

        void selection_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            this.txtSelectedUserNum.Text = selection.SelectedCount.ToString() + "个用户已选中";
            lbSelectedUsers.Items.Clear();
            foreach (object o in selection.Selection)
            {
                User_Info info = o as User_Info;
                if (null != info)
                    lbSelectedUsers.Items.Add(info.user_name);
            }
        }

        private void SetGroupChecked(IList<string> users, int groupid)
        {
            UserGroupController uc = new UserGroupController();
            IList<User_Group_Relation> rels = uc.GetUserRelsByGroupID(groupid);
            IList<string> groupAuthed = new List<string>();
            rels.ForEach(r => groupAuthed.Add(r.user_id));

            for (int i = 0; i < users.Count; i++)
            {
                if (groupAuthed.Contains(users[i]))
                    selection.SelectRow(i, true);
            }
        }

        private bool CheckInput()
        {
            if (string.IsNullOrEmpty(this.txtGroupName.Text))
            {
                ExMessageBox.Show("提示信息：角色名称不能为空！", AppSetter.SoftName);
                return false;
            } 

            return true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckInput())
                return;
            IList<string> userids = new List<string>();
            foreach (object o in selection.Selection)
            {
                User_Info info = o as User_Info;
                if (null != info)
                    userids.Add(info.user_id);
            }
            try
            {
                SetRoleUserController gc = new SetRoleUserController();
                gc.SetRoleUser(this.CurrentUserGroup.group_id,userids);
                LogHelper.Log("设定用户", "update", "group_id:" + this.CurrentUserGroup.group_id, "userids:" + string.Join(",", userids));
                ExMessageBox.Show("提示信息：保存成功！", AppSetter.SoftName);
                this.Close();
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }

    }
}
